/**
 * @description 贝壳找房小区搜索爬虫 - 优化版
 */
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const fs = require('fs');
const path = require('path');

// 使用隐身插件
puppeteer.use(StealthPlugin());

/**
 * @description 获取当前日期格式化字符串，用于创建日期目录
 * @returns {string} 格式化的日期字符串（YYYY-MM-DD）
 */
function getFormattedDate() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

/**
 * @description 确保输出目录存在
 * @param {string} dateDir - 日期目录名
 * @param {string} communityName - 小区名称
 * @returns {Object} 包含各种输出目录路径的对象
 */
function ensureOutputDir(dateDir, communityName) {
  // 创建基础output目录（如果不存在）
  const baseOutputDir = 'output';
  if (!fs.existsSync(baseOutputDir)) {
    fs.mkdirSync(baseOutputDir);
    console.log(`创建基础输出目录: ${baseOutputDir}`);
  }
  
  // 创建日期子目录
  const dateOutputDir = path.join(baseOutputDir, dateDir);
  if (!fs.existsSync(dateOutputDir)) {
    fs.mkdirSync(dateOutputDir);
    console.log(`创建日期输出目录: ${dateOutputDir}`);
  }
  
  // 创建安全的小区名称（用于目录名）
  const safeCommunityName = communityName.replace(/[\\/:*?"<>|]/g, '_');
  
  // 创建小区目录
  const communityDir = path.join(dateOutputDir, safeCommunityName);
  if (!fs.existsSync(communityDir)) {
    fs.mkdirSync(communityDir);
    console.log(`创建小区输出目录: ${communityDir}`);
  }
  
  // 创建截图子目录
  const screenshotsDir = path.join(communityDir, 'screenshots');
  if (!fs.existsSync(screenshotsDir)) {
    fs.mkdirSync(screenshotsDir);
    console.log(`创建截图目录: ${screenshotsDir}`);
  }
  
  return {
    baseDir: baseOutputDir,
    dateDir: dateOutputDir,
    communityDir: communityDir,
    screenshotsDir: screenshotsDir,
    safeCommunityName: safeCommunityName
  };
}

/**
 * @description 处理爬取结果，提取车位信息并保存到单独文件
 * @param {Object} data - 爬取结果数据
 * @param {string} communityName - 小区名称
 * @param {string} outputDir - 输出目录
 */
function processParkingSpaces(data, communityName, outputDir) {
  console.log(`开始处理车位数据...`);
  
  if (!data || !data.houses || !Array.isArray(data.houses) || data.houses.length === 0) {
    console.log('没有可处理的房源数据');
    return;
  }
  
  // 筛选标题中包含"车位"的房源
  const parkingSpaces = data.houses.filter(house => {
    return house.title && (house.title.includes('车位') || house.title.includes('停车') || house.title.includes('车库'));
  });
  
  console.log(`找到${parkingSpaces.length}个车位信息`);
  
  if (parkingSpaces.length === 0) {
    console.log('未找到车位信息');
    return;
  }
  
  // 计算统计数据
  const statistics = calculateStatistics(parkingSpaces);
  
  // 创建输出数据
  const outputData = {
    communityName: communityName,
    title: `${communityName}车位信息`,
    crawlTime: data.crawlTime,
    totalParkingSpaces: parkingSpaces.length,
    totalElement: statistics,
    parkingSpaces: parkingSpaces
  };
  
  // 创建文件路径
  const filePath = path.join(outputDir, `parking_spaces.json`);
  
  fs.writeFileSync(filePath, JSON.stringify(outputData, null, 2), {encoding: 'utf-8'});
  console.log(`已将${parkingSpaces.length}个车位信息保存到${filePath}`);
}

/**
 * @description 处理爬取结果，提取最低单价的房源
 * @param {Object} data - 爬取结果数据
 * @param {string} communityName - 小区名称
 * @param {string} outputDir - 输出目录
 * @param {number} limit - 提取数量，默认为10
 */
function processLowPriceHouses(data, communityName, outputDir, limit = 10) {
  console.log(`开始处理低单价房源数据...`);
  
  if (!data || !data.houses || !Array.isArray(data.houses) || data.houses.length === 0) {
    console.log('没有可处理的房源数据');
    return;
  }
  
  // 再次检查是否是目标小区的房源，并排除车位
  const targetHouses = data.houses.filter(house => {
    // 排除标题中包含"车位"的房源
    if (house.title && (house.title.includes('车位') || house.title.includes('停车') || house.title.includes('车库'))) {
      return false;
    }
    
    const { title, position, houseInfo } = house;
    const isTargetCommunity = 
      (position && position.includes(communityName)) || 
      (title && title.includes(communityName)) || 
      (houseInfo && houseInfo.includes(communityName));
    return isTargetCommunity;
  });
  
  console.log(`过滤后有效${communityName}小区房源数量(不含车位): ${targetHouses.length}`);
  
  // 提取有效单价的房源（过滤掉没有单价信息的）
  const housesWithPrice = targetHouses.filter(house => {
    // 检查是否有单价字段
    if (!house.unitPrice) return false;
    
    // 提取数字部分
    const priceMatch = house.unitPrice.match(/\d+/);
    return priceMatch && !isNaN(parseInt(priceMatch[0]));
  });
  
  if (housesWithPrice.length === 0) {
    console.log('没有找到有效单价的房源');
    return;
  }
  
  // 排序函数：提取单价中的数字并按升序排序
  const sortedHouses = [...housesWithPrice].sort((a, b) => {
    const priceA = parseInt(a.unitPrice.match(/\d+/)[0]);
    const priceB = parseInt(b.unitPrice.match(/\d+/)[0]);
    return priceA - priceB;
  });
  
  // 提取前limit个（或全部，如果数量不足）
  const lowPriceHouses = sortedHouses.slice(0, Math.min(limit, sortedHouses.length));
  
  // 计算低单价房源的统计数据
  const statistics = calculateStatistics(lowPriceHouses);
  console.log('低单价房源统计数据已生成');
  
  // 创建输出数据
  const outputData = {
    communityName: communityName,
    title: `${communityName}最低单价${limit}套房源`,
    crawlTime: data.crawlTime,
    totalProcessed: housesWithPrice.length,
    totalElement: statistics, // 添加统计数据
    houses: lowPriceHouses
  };
  
  // 创建文件路径 - 简化文件名
  const filePath = path.join(outputDir, `low_price.json`);
  
  fs.writeFileSync(filePath, JSON.stringify(outputData, null, 2), {encoding: 'utf-8'});
  console.log(`已将${lowPriceHouses.length}套低单价房源保存到${filePath}`);
}

/**
 * @description 统计房源数据，生成统计结果
 * @param {Array} houses - 房源数据数组
 * @returns {Object} 统计结果
 */
function calculateStatistics(houses) {
  console.log(`开始统计房源数据...`);
  
  if (!houses || !Array.isArray(houses) || houses.length === 0) {
    console.log('没有可统计的房源数据');
    return {};
  }

  // 初始化统计数据
  const stats = {
    totalCount: houses.length,
    priceStats: {
      minPrice: Number.MAX_VALUE,
      maxPrice: 0,
      avgPrice: 0,
      totalPrice: 0
    },
    unitPriceStats: {
      minUnitPrice: Number.MAX_VALUE,
      maxUnitPrice: 0,
      avgUnitPrice: 0,
      totalUnitPrice: 0,
      validUnitPriceCount: 0
    },
    houseTypes: {},
    floorDistribution: {},
    ageDistribution: {
      "5年内": 0,
      "5-10年": 0,
      "10-20年": 0,
      "20年以上": 0,
      "未知": 0
    }
  };

  // 遍历房源进行统计
  houses.forEach(house => {
    // 总价统计
    if (house.totalPrice) {
      const price = parseFloat(house.totalPrice.replace(/[^0-9.]/g, ''));
      if (!isNaN(price)) {
        stats.priceStats.totalPrice += price;
        stats.priceStats.minPrice = Math.min(stats.priceStats.minPrice, price);
        stats.priceStats.maxPrice = Math.max(stats.priceStats.maxPrice, price);
      }
    }

    // 单价统计
    if (house.unitPrice) {
      const unitPrice = parseInt(house.unitPrice.match(/\d+/)?.[0] || '0');
      if (unitPrice > 0) {
        stats.unitPriceStats.totalUnitPrice += unitPrice;
        stats.unitPriceStats.minUnitPrice = Math.min(stats.unitPriceStats.minUnitPrice, unitPrice);
        stats.unitPriceStats.maxUnitPrice = Math.max(stats.unitPriceStats.maxUnitPrice, unitPrice);
        stats.unitPriceStats.validUnitPriceCount++;
      }
    }

    // 房型统计 (如: 2室1厅)
    if (house.houseInfo) {
      const houseTypeMatch = house.houseInfo.match(/(\d+室\d+厅)/);
      if (houseTypeMatch) {
        const houseType = houseTypeMatch[1];
        stats.houseTypes[houseType] = (stats.houseTypes[houseType] || 0) + 1;
      }
      
      // 楼层统计
      const floorMatch = house.houseInfo.match(/(低|中|高)楼层/);
      if (floorMatch) {
        const floor = floorMatch[1] + '楼层';
        stats.floorDistribution[floor] = (stats.floorDistribution[floor] || 0) + 1;
      }
      
      // 房龄统计
      if (house.houseInfo.includes('建筑年代')) {
        const yearMatch = house.houseInfo.match(/(\d{4})年/);
        if (yearMatch) {
          const buildYear = parseInt(yearMatch[1]);
          const currentYear = new Date().getFullYear();
          const age = currentYear - buildYear;
          
          if (age <= 5) {
            stats.ageDistribution["5年内"]++;
          } else if (age <= 10) {
            stats.ageDistribution["5-10年"]++;
          } else if (age <= 20) {
            stats.ageDistribution["10-20年"]++;
          } else {
            stats.ageDistribution["20年以上"]++;
          }
        } else {
          stats.ageDistribution["未知"]++;
        }
      } else {
        stats.ageDistribution["未知"]++;
      }
    }
  });

  // 计算平均值
  if (houses.length > 0) {
    stats.priceStats.avgPrice = (stats.priceStats.totalPrice / houses.length).toFixed(2);
  }
  
  if (stats.unitPriceStats.validUnitPriceCount > 0) {
    stats.unitPriceStats.avgUnitPrice = Math.round(stats.unitPriceStats.totalUnitPrice / stats.unitPriceStats.validUnitPriceCount);
  }

  // 处理边界情况
  if (stats.priceStats.minPrice === Number.MAX_VALUE) {
    stats.priceStats.minPrice = 0;
  }
  
  if (stats.unitPriceStats.minUnitPrice === Number.MAX_VALUE) {
    stats.unitPriceStats.minUnitPrice = 0;
  }

  console.log(`统计完成: ${houses.length}套房源`);
  return stats;
}

/**
 * @description 根据小区名称搜索并爬取房源数据
 * @param {string} communityName - 小区名称
 * @param {number} maxPages - 最大爬取页数，设为0则爬取所有页面
 * @returns {Promise<Array>} 房屋数据数组
 */
async function searchAndCrawl(communityName, maxPages = 0) {
  console.log(`开始搜索小区: ${communityName}`);
  const browser = await puppeteer.launch({
    headless: false,
    slowMo: 100,
    args: ['--no-sandbox', '--disable-setuid-sandbox']
  });

  const page = await browser.newPage();
  
  // 设置用户代理
  await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
  
  // 存储结果
  const results = [];
  
  try {
    // 先访问首页
    console.log('访问贝壳找房首页...');
    await page.goto('https://cd.ke.com/ershoufang/', {
      waitUntil: 'networkidle2',
      timeout: 60000
    });
    
    // 获取当前日期并创建输出目录
    const dateDir = getFormattedDate();
    const outputDir = ensureOutputDir(dateDir, communityName);
    const screenshotsDir = outputDir.screenshotsDir;
    
    // 创建安全的文件名
    const safeFileName = outputDir.safeCommunityName;
    
    // 截取首页图片
    await page.screenshot({ 
      path: path.join(screenshotsDir, 'homepage.png'), 
      fullPage: true 
    });
    console.log('已保存首页截图');
    
    // 检查是否需要人机验证
    const captchaExists = await page.evaluate(() => {
      return document.body.innerText.includes('人机验证') || 
             document.body.innerText.includes('CAPTCHA');
    });
    
    if (captchaExists) {
      console.log('遇到人机验证，等待手动处理...');
      await new Promise(resolve => setTimeout(resolve, 30000));
    }
    
    // 使用正确的搜索框ID
    console.log('定位搜索框...');
    const searchInputExists = await page.evaluate(() => {
      return document.querySelector('#searchInput') !== null;
    });
    
    if (searchInputExists) {
      // 在搜索框中输入小区名称
      console.log(`在搜索框输入小区名称: ${communityName}`);
      await page.waitForSelector('#searchInput', { visible: true, timeout: 10000 });
      
      // 清空搜索框中可能存在的默认内容
      await page.evaluate(() => {
        document.querySelector('#searchInput').value = '';
      });
      
      await page.focus('#searchInput');
      await page.type('#searchInput', communityName, { delay: 100 });
      
      // 查找搜索按钮并点击
      console.log('点击搜索按钮...');
      // 尝试各种可能的搜索按钮选择器
      const buttonSelectors = [
        '.search-button',
        '.index-search-btn',
        'button[data-action="search"]',
        '#searchForm .search',
        'form[action*="search"] button',
        '#searchForm button'
      ];
      
      let buttonFound = false;
      for (const selector of buttonSelectors) {
        const buttonExists = await page.evaluate((sel) => {
          return document.querySelector(sel) !== null;
        }, selector);
        
        if (buttonExists) {
          console.log(`找到搜索按钮: ${selector}`);
          await page.click(selector);
          buttonFound = true;
          break;
        }
      }
      
      if (!buttonFound) {
        console.log('未找到搜索按钮，尝试按回车键提交');
        await page.keyboard.press('Enter');
      }
    } else {
      // 如果找不到搜索框，直接构建搜索URL
      console.log('未找到搜索框，直接构建搜索URL');
      // 使用更精确的搜索参数：rs表示搜索关键词，c表示搜索类型(小区)
      const searchUrl = `https://cd.ke.com/ershoufang/rs${encodeURIComponent(communityName)}/c${encodeURIComponent(communityName)}/`;
      await page.goto(searchUrl, {
        waitUntil: 'networkidle2',
        timeout: 60000
      });
    }
    
    // 等待搜索结果加载
    await new Promise(resolve => setTimeout(resolve, 5000));
    console.log('等待搜索结果加载完成...');
    
    // 保存搜索结果页面截图，便于调试
    const searchResultScreenshot = path.join(screenshotsDir, `${safeFileName}_search_results.png`);
    await page.screenshot({ path: searchResultScreenshot, fullPage: true });
    console.log(`已保存搜索结果截图: ${searchResultScreenshot}`);
    
    // 检查是否有搜索结果
    const noResult = await page.evaluate(() => {
      return document.querySelector('.no-result-wrapper') !== null;
    });
    
    if (noResult) {
      console.log(`未找到"${communityName}"相关房源`);
      return [];
    }
    
    console.log('搜索结果已加载，获取总房源数和页数...');
    
    // 获取总房源数和总页数
    const { totalHouses, totalPages } = await page.evaluate(() => {
      // 尝试获取总房源数
      let totalHouses = 0;
      const totalElements = document.querySelectorAll('.total span');
      if (totalElements && totalElements.length > 0) {
        const totalText = totalElements[0].innerText;
        const match = totalText.match(/\d+/);
        if (match) {
          totalHouses = parseInt(match[0]);
        }
      }
      
      // 获取总页数
      let totalPages = 1;
      const pageInfo = document.querySelector('.page-box');
      if (pageInfo) {
        // 方法1: 通过data属性
        const totalData = pageInfo.getAttribute('data-total-count');
        if (totalData) {
          const total = parseInt(totalData);
          totalPages = Math.ceil(total / 30) || 1; // 假设每页30条数据
        } else {
          // 方法2: 通过页码列表
          const pageLinks = document.querySelectorAll('.page-box a');
          if (pageLinks && pageLinks.length > 0) {
            // 尝试从最后一个页码链接获取
            const lastPageText = pageLinks[pageLinks.length - 1].innerText;
            const pageMatch = lastPageText.match(/\d+/);
            if (pageMatch) {
              totalPages = parseInt(pageMatch[0]);
            }
          }
        }
      }
      
      return { totalHouses, totalPages };
    });
    
    console.log(`找到${totalHouses}套房源，共${totalPages}页`);
    
    // 决定要爬取的页数
    const actualPages = maxPages > 0 ? Math.min(maxPages, totalPages) : totalPages;
    console.log(`计划爬取${actualPages}页数据`);
    
    // 爬取每一页
    for (let i = 1; i <= actualPages; i++) {
      console.log(`正在爬取第${i}/${actualPages}页...`);
      
      if (i > 1) {
        try {
          // 通过点击分页按钮而不是修改URL来导航
          console.log(`尝试点击第${i}页的分页按钮...`);
          
          // 等待分页按钮加载
          await page.waitForSelector('.page-box a', { timeout: 10000 });
          
          // 查找当前页的页码按钮
          const foundPageButton = await page.evaluate((pageNum) => {
            // 寻找所有页码按钮
            const pageButtons = Array.from(document.querySelectorAll('.page-box a'));
            
            // 尝试找到指定页码的按钮
            for (const button of pageButtons) {
              if (button.innerText.trim() === pageNum.toString()) {
                return true;
              }
            }
            
            return false;
          }, i);
          
          if (foundPageButton) {
            // 点击页码按钮
            await page.evaluate((pageNum) => {
              const pageButtons = Array.from(document.querySelectorAll('.page-box a'));
              for (const button of pageButtons) {
                if (button.innerText.trim() === pageNum.toString()) {
                  button.click();
                  return;
                }
              }
            }, i);
            
            console.log(`已点击第${i}页按钮`);
          } else {
            // 如果找不到特定页码的按钮，尝试点击"下一页"按钮
            console.log(`未找到第${i}页按钮，尝试点击"下一页"按钮`);
            const nextButtonExists = await page.evaluate(() => {
              const nextButtons = Array.from(document.querySelectorAll('.page-box a'));
              for (const button of nextButtons) {
                if (button.innerText.includes('下一页') || button.innerText.includes('下页') || button.title.includes('下一页')) {
                  button.click();
                  return true;
                }
              }
              return false;
            });
            
            if (!nextButtonExists) {
              // 如果找不到下一页按钮，则尝试使用URL方式
              console.log('未找到下一页按钮，尝试使用URL方式导航');
              // 构建页码URL
              const currentUrl = page.url();
              // 处理URL格式，确保正确构建分页URL
              let baseUrl;
              if (currentUrl.includes('pg')) {
                // URL已经包含页码，替换它
                baseUrl = currentUrl.replace(/pg\d+/, '');
              } else {
                baseUrl = currentUrl;
              }
              
              // 确保URL以/结尾
              const formattedBaseUrl = baseUrl.endsWith('/') ? baseUrl : baseUrl + '/';
              const pageUrl = `${formattedBaseUrl}pg${i}/`;
              
              console.log(`导航到页面: ${pageUrl}`);
              await page.goto(pageUrl, {
                waitUntil: 'networkidle2',
                timeout: 60000
              });
            }
          }
          
          // 等待页面加载
          console.log('等待页面加载...');
          await new Promise(resolve => setTimeout(resolve, 5000));
          await page.waitForSelector('.sellListContent, ul.listContent, .house-list', { timeout: 15000 });
          
        } catch (error) {
          console.error(`导航到第${i}页时出错:`, error.message);
          // 如果按钮点击方式失败，回退到URL导航方式
          console.log('尝试使用URL方式导航');
          try {
            const currentUrl = page.url();
            let baseUrl = currentUrl;
            if (currentUrl.includes('pg')) {
              baseUrl = currentUrl.replace(/pg\d+/, '');
            }
            const formattedBaseUrl = baseUrl.endsWith('/') ? baseUrl : baseUrl + '/';
            const pageUrl = `${formattedBaseUrl}pg${i}/`;
            
            console.log(`导航到页面: ${pageUrl}`);
            await page.goto(pageUrl, {
              waitUntil: 'networkidle2',
              timeout: 60000
            });
            
            // 等待页面加载
            await new Promise(resolve => setTimeout(resolve, 3000));
          } catch (navError) {
            console.error(`使用URL导航到第${i}页也失败:`, navError.message);
            continue; // 跳过这一页
          }
        }
      }
      
      // 等待房源列表加载
      let listLoaded = false;
      for (const selector of ['.sellListContent', 'ul.listContent', '.house-list']) {
        try {
          await page.waitForSelector(selector, { timeout: 10000 });
          console.log(`找到房源列表选择器: ${selector}`);
          listLoaded = true;
          break;
        } catch (error) {
          console.log(`未找到选择器: ${selector}`);
        }
      }
      
      if (!listLoaded) {
        console.log(`第${i}页未找到房源列表，可能页面结构变化`);
        // 保存错误页面截图
        const errorScreenshot = path.join(screenshotsDir, `${safeFileName}_error_page_${i}.png`);
        await page.screenshot({ path: errorScreenshot, fullPage: true });
        console.log(`已保存错误页面截图: ${errorScreenshot}`);
        continue; // 跳过当前页，继续下一页
      }
      
      // 截取当前页面图片
      const pageScreenshot = path.join(screenshotsDir, `${safeFileName}_page_${i}.png`);
      await page.screenshot({ path: pageScreenshot, fullPage: true });
      console.log(`已保存第${i}页截图: ${pageScreenshot}`);
      
      // 提取数据
      const houses = await page.evaluate((targetCommunity) => {
        // 尝试多种可能的选择器来定位房源列表项
        const selectors = [
          '.sellListContent li',
          '.sellListContent .VIEWDATA',
          'ul.listContent li',
          '.house-list li'
        ];
        
        let items = [];
        for (const selector of selectors) {
          const elements = document.querySelectorAll(selector);
          if (elements && elements.length > 0) {
            items = Array.from(elements);
            break;
          }
        }
        
        const houseList = [];
        
        items.forEach(item => {
          try {
            // 尝试多种可能的选择器
            const infoDiv = item.querySelector('.info') || item;
            if (!infoDiv) return;
            
            // 提取标题
            const titleEl = infoDiv.querySelector('.title a') || infoDiv.querySelector('.TITLE a');
            const title = titleEl?.innerText || '';
            
            // 提取位置
            const positionEl = infoDiv.querySelector('.positionInfo') || infoDiv.querySelector('.flood');
            const position = positionEl?.innerText || '';
            
            // 提取房屋信息
            const houseInfoEl = infoDiv.querySelector('.houseInfo') || infoDiv.querySelector('.address');
            const houseInfo = houseInfoEl?.innerText || '';
            
            // 提取关注和发布时间
            const followInfoEl = infoDiv.querySelector('.followInfo');
            const followInfo = followInfoEl?.innerText || '';
            
            // 提取标签
            const tagEls = infoDiv.querySelectorAll('.tag span') || infoDiv.querySelectorAll('.tag');
            const tags = Array.from(tagEls).map(tag => tag.innerText).join(',');
            
            // 提取总价
            const totalPriceEl = infoDiv.querySelector('.totalPrice span') || infoDiv.querySelector('.priceInfo .total');
            const totalPrice = totalPriceEl?.innerText || '';
            
            // 提取单价
            const unitPriceEl = infoDiv.querySelector('.unitPrice span') || infoDiv.querySelector('.priceInfo .unit');
            const unitPrice = unitPriceEl?.innerText || '';
            
            // 提取房源链接
            const link = titleEl?.href || '';
            
            // 检查是否属于目标小区
            const isTargetCommunity = 
              (position && position.includes(targetCommunity)) || 
              (title && title.includes(targetCommunity)) ||
              (houseInfo && houseInfo.includes(targetCommunity));
            
            if (isTargetCommunity) {
              houseList.push({
                title,
                position,
                houseInfo,
                followInfo,
                tags,
                totalPrice: totalPrice ? totalPrice + '万' : '',
                unitPrice,
                link
              });
            }
          } catch (e) {
            // 忽略单个房源解析错误
          }
        });
        
        return houseList;
      }, communityName);
      
      console.log(`第${i}页找到${houses.length}条符合"${communityName}"小区的房源`);
      results.push(...houses);
      
      // 随机等待，避免被反爬
      const randomWait = Math.floor(Math.random() * 2000) + 3000;
      console.log(`随机等待${randomWait/1000}秒...`);
      await new Promise(resolve => setTimeout(resolve, randomWait));
    }
    
    console.log('爬取完成!');
    
    // 计算统计数据
    const statistics = calculateStatistics(results);
    console.log('房源统计数据已生成');
    
    // 保存数据到文件，添加元数据
    const resultData = {
      communityName: communityName,
      totalHouses: totalHouses,
      actualCrawled: results.length,
      totalPages: totalPages,
      actualPages: actualPages,
      crawlTime: new Date().toISOString(),
      totalElement: statistics, // 添加统计数据
      houses: results
    };
    
    const filePath = path.join(outputDir.communityDir, `houses.json`);
    
    fs.writeFileSync(filePath, JSON.stringify(resultData, null, 2), {encoding: 'utf-8'});
    console.log(`共爬取${results.length}条房源数据，已保存到${filePath}`);
    
    // 处理低单价房源数据
    processLowPriceHouses(resultData, communityName, outputDir.communityDir);
    
    // 处理车位数据
    processParkingSpaces(resultData, communityName, outputDir.communityDir);
    
    return results;
  } catch (error) {
    console.error('爬取过程中出错:', error);
    // 保存错误截图
    try {
      const dateDir = getFormattedDate();
      const outputDir = ensureOutputDir(dateDir, communityName);
      const screenshotsDir = outputDir.screenshotsDir;
      const errorScreenshot = path.join(screenshotsDir, `error.png`);
      await page.screenshot({ path: errorScreenshot, fullPage: true });
      console.log(`错误页面已保存为${errorScreenshot}`);
    } catch (e) {}
    return [];
  } finally {
    console.log('正在关闭浏览器...');
    await browser.close();
    console.log('浏览器已关闭');
  }
}

/**
 * @description 程序入口
 */
(async () => {
  try {
    // 从命令行获取参数
    const args = process.argv.slice(2);
    let communityName = '会所花园'; // 默认值
    let maxPages = 0; // 默认爬取所有页面
    
    // 解析命令行参数
    if (args.length >= 1) {
      communityName = args[0];
    }
    
    if (args.length >= 2) {
      maxPages = parseInt(args[1], 10);
      if (isNaN(maxPages)) {
        maxPages = 0;
      }
    }
    
    console.log(`将爬取小区: ${communityName}, 最大页数: ${maxPages === 0 ? '不限' : maxPages}`);
    await searchAndCrawl(communityName, maxPages);
  } catch (err) {
    console.error('程序执行出错:', err);
  }
})();